﻿using AiChiTu.TraceabilitySystem.Domain.AiChiTuManagement;
using AiChiTu.TraceabilitySystem.ErrorCode;
using AiChiTu.TraceabilitySystem.ErrorCode.Extensions;
using AiChiTu.TraceabilitySystem.Infrastructure.Interfaces;
using AiChiTu.TraceabilitySystem.Write.API.Applications.Commands.ProductBasices;
using AutoMapper;
using MediatR;

namespace AiChiTu.TraceabilitySystem.Write.API.Applications.CommandsHandlers.ProductBasices
{
	public class DeletedProductBasicsCommandHandle : IRequestHandler<DeletedProductBasicsCommand, APIResult<bool>>
	{
		private readonly IBaseRepository<ProductBasics> productbasicRepository;

		public DeletedProductBasicsCommandHandle(IBaseRepository<ProductBasics> productbasicRepository)
		{
			this.productbasicRepository = productbasicRepository;
		}
		/// <summary>
		/// 批量删除商品基础信息请求命令处理
		/// </summary>
		/// <param name="request">批量删除商品基础信息请求命令</param>
		/// <param name="cancellationToken">取消</param>
		/// <returns>返回任务</returns>
		public async Task<APIResult<bool>> Handle(DeletedProductBasicsCommand request, CancellationToken cancellationToken)
		{
			APIResult<bool> result = new APIResult<bool>();
			result.Code = ResultCode.DeletedFailed;
			result.Message = ResultCode.DeletedFailed.GetDescription();
			result.Data = false;

			List<ProductBasics> list = productbasicRepository.QueryAllAsync(x => request.ProductBasicsIdsstring.Contains(x.ProductBasicsId.ToString())).ToList();
			foreach (ProductBasics item in list)
			{
				item.IsDeleted = true;
			}
			if (await productbasicRepository.UpdateEntitiesAsync(list))
			{
				result.Code = ResultCode.DeletedSuccess;
				result.Message = ResultCode.DeletedSuccess.GetDescription();
				result.Data = true;
			}

			return await Task.FromResult(result);
		}
	}
}
